/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootVersion;
import org.springframework.boot.ansi.AnsiColor;
import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 该注解指定项目为springboot，由此类当作程序入口 自动装配 web 依赖的环境
 * 
 **/
@SpringBootApplication
//@EnableAutoConfiguration
@EnableScheduling
@ComponentScan(value = { "org.swan", "com.koron", "com.koron.main" })
public class App extends SpringBootServletInitializer {

	public static void main(String[] args) throws Exception {
		SpringApplication app = new SpringApplication(App.class);
        if (System.getenv("CONFIG_CENTER_URL") != null) {
            System.out.println("加载配置文件");
            Properties p = new Properties();
            URL u = new URL(System.getenv("CONFIG_CENTER_URL") + System.getenv("CONFIG_PATH") + ".htm");
            ObjectMapper mapper = new ObjectMapper();
            HashMap<String, Object> map = (HashMap) mapper.readValue(u, mapper.getTypeFactory().constructParametricType(HashMap.class, new Class[]{String.class, Object.class}));
            if ("0".equals(String.valueOf(map.get("code")))) {
                p.putAll((Map) map.get("data"));
            }
            app.setDefaultProperties(p);
        }
        Banner b = (environment, sourceClass, printStream) -> {
            BufferedReader reader = new BufferedReader(new InputStreamReader(App.class.getClassLoader().getResourceAsStream("logo.txt")));
            String tmp = null;
            try {
                while ((tmp = reader.readLine()) != null)
                    printStream.println(tmp);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            printStream.print("App version is:" + AnsiOutput.toString(AnsiColor.RED, environment.getProperty("app.version")) + "  ");
            String version = SpringBootVersion.getVersion();
            version = (version == null ? "" : " (v" + version + ")");
            printStream.println(AnsiOutput.toString(AnsiColor.GREEN, "Spring Boot:" + version));
        };
        app.setBanner(b);
        app.run(args);
	}
}